#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-06-07 15:08:38
# @Author  : rwecho rwecho@126.com
# @Description    :

from flask import Flask
from flask import jsonify
from flask_cors import CORS
from myjobMonitor import monitor
from myblog import blog
import os
from flask import send_from_directory

app = Flask(__name__)
CORS(app, supports_credentials=True)  # enable CORS
app.register_blueprint(monitor, url_prefix='/monitor')
app.register_blueprint(blog, url_prefix='/blog')

from flask import request, current_app


def after_request_func(response):
    callback = request.args.get('callback', False)
    if callback:
        data = response.data
        data = data.decode('utf-8')  # replace str() with decode. convert string to utf8 string not bytes string.
        content = str(callback) + '(' + data + ')'
        mimetype = 'application/javascript'
        return current_app.response_class(content, mimetype=mimetype)
    else:
        return response


app.after_request(after_request_func)


@app.route('/')
def home():
    data = {
        'abc': '123'
    }
    return jsonify(data)
    # return 'hello world'


@app.route('/favicon.ico')
def favicon():
    image_path = os.path.join(app.root_path, 'myjobMonitor/static/images')
    print(image_path)
    return send_from_directory(image_path, 'favicon.ico')


@app.errorhandler(404)
def page_not_found(e):
    return '404'


if __name__ == '__main__':
    app.run(debug=True, port=5000)
